import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_xupdate/flutter_xupdate.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/dialog/dialog_util.dart';
import 'package:industry_net/base/dioUtil/http_man.dart';
import 'package:industry_net/base/dioUtil/net_api.dart';
import 'package:industry_net/pages/me/main/models/version_update_model.dart';
import 'package:industry_net/pages/msg/chat/im.dart';
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';

initalizeView() {
  //设置导航栏
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      systemNavigationBarColor: Color(0xFF000000),
      systemNavigationBarDividerColor: null,
      statusBarColor: Colors.transparent,
      systemNavigationBarIconBrightness: Brightness.dark,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
    ),
  );
  // IMManager.instance.register();
  // IMManager.instance.login();

  // Future.delayed(const Duration(seconds: 1)).then((value) {
  //   initXUpdate();
  // });
  // versionUpdate();
}

///初始化
void initXUpdate() {
  if (Platform.isAndroid) {
    FlutterXUpdate.init(

            ///是否输出日志
            debug: kDebugMode,

            ///是否使用post请求
            isPost: false,

            ///post请求是否是上传json
            isPostJson: false,

            ///请求响应超时时间
            timeout: 25000,

            ///是否开启自动模式
            isWifiOnly: false,

            ///是否开启自动模式
            isAutoMode: false,

            ///需要设置的公共参数
            supportSilentInstall: false,

            ///在下载过程中，如果点击了取消的话，是否弹出切换下载方式的重试提示弹窗
            enableRetry: false)
        .then((value) {
      EasyLoading.showToast("初始化成功: $value");
    }).catchError((error) {
      print(error);
    });
  } else {
    // EasyLoading.showToast("ios暂不支持XUpdate更新");
  }
}

void versionUpdate() {
  HttpMan()
      .get(
    Api.versionUpdate,
    needBaseResp: false,
  )
      .then((res) {
    var _data = jsonDecode(res.data);
    var md = VersionUpdateModel.fromJson(_data);
    if (md.code == 0) {
      PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
        var _currVersion = packageInfo.version;
        var res = md.versionName!.compareTo(_currVersion);
        if (res == 0) {
          showAlert(
            title: '更新提示',
            msg: md.modifyContent,
            sureAction: () {
              if (md.appleUrl != null) {
                launch(md.appleUrl!);
                // launch(
                //     'https://apps.apple.com/cn/app/apple-store/id333206289');
              }
            },
          );
        }
      });
    }
  });
}
